package Server.Init;

import java.io.*;
import java.net.*;

public class ServerSession {

    private static int portServer = 9000;
    private static boolean serverActive = true;
    private static ServerSocket serverSocket;

    public ServerSession() {
    }

    public static void main(String args[]) {
        ServerSession session = new ServerSession();
        session.startServer();
    }

    public void startServer() {
        Socket client = null;
        try {
            serverSocket = new ServerSocket(portServer);
            while (serverActive) {
                client = serverSocket.accept();
                ServerThread thread = new ServerThread(client);
                thread.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (serverSocket != null) {
                    serverSocket.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public void closeServer() {
        serverActive = false;
    }
}
